package com.mayisoftware.mayioa.platform.impl.task;

import com.mayisoftware.mayioa.platform.api.domain.SysMail;
import com.mayisoftware.mayioa.platform.api.service.ISysMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("mailSendTask")
public class MailSendTask {

    /**
     * 日志对象
     */
    private static final Logger log = LoggerFactory.getLogger(MailSendTask.class);

    /**
     * 邮件服务
     */
    @Autowired
    ISysMailService sysMailService;

    public void sendMail(){
//        log.info("发送邮件开始");
        try{
            List<SysMail> sysMails = sysMailService.selectUnsendMailList();
            if(sysMails != null && !sysMails.isEmpty()){
                for(SysMail sysMail :sysMails){
                    sysMailService.sendSimpleEmail(sysMail.getMailReceiver(), sysMail.getMailTitle(),sysMail.getMailContent());
                    sysMailService.updateMailToSend(sysMail);
                    log.info("发送邮件."+sysMail.getMailRecordId());
                }
            }else{
//                log.info("没有需要发送的邮件");
            }
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
//        log.info("发送邮件结束");
    }
}
